package com.wu01.demo.newdemo.controller;

import com.example.java_learning.entity.Order;
import com.example.java_learning.service.UploadService;
import com.example.java_learning.util.EasyExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wubingcang
 * @desc
 * @date 2024年04月21日 7:50
 */
@RestController
public class EasyExcelController {
    @Autowired
    private UploadService uploadService;

    @GetMapping("download")
    public void download(HttpServletResponse response){
        List<Order> orders = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Order order = new Order();
            order.setPayNo("order-"+i);
            order.setCount(i);
            order.setLocalDateTime(LocalDateTime.now());
            order.setOrderName("order");
            order.setSignAgreement(true);
            order.setPayAmount(new BigDecimal("1.12343").multiply(BigDecimal.valueOf(i)));
            orders.add(order);
        }
        try {
            EasyExcelUtils.download(response,"测试文件",Order.class,orders);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @PostMapping("upload")
    @ResponseBody
    public String upload(MultipartFile file){
        try {
            return EasyExcelUtils.upload(file,Order.class,uploadService);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "failed";
    }
}
